package com.esunny.db.hbase;

import java.util.Properties;
import java.util.Vector;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        if (args.length != 7) {
            System.out.println("Please specify a actionName, threadcount, tableName,columnfamily , operation count,fieldSize,valueSize");
            System.exit(0);
        }

        final int keyspace = 300000000; // 120000000;
        final String actionName = args[0];
        final int threadcount = Integer.parseInt(args[1]);
        final String tableName = args[2];
        final String columnfamily = args[3];

        final int opcount = Integer.parseInt(args[4]) / threadcount;
        final int fieldSize = Integer.parseInt(args[5]);
        final int valueSize = Integer.parseInt(args[6]);

        Vector<Thread> allthreads = new Vector<Thread>();

        for (int i = 0; i < threadcount; i++) {
            Thread t = new Thread() {

                public void run() {
                    try {

                        Action action = ActionFactory.getAction(actionName, tableName, keyspace, valueSize, fieldSize);
                        Properties props = new Properties();
                        props.setProperty("debug", "false");
                        props.setProperty("columnfamily", columnfamily);
                        action.init(props);

                        long accum = 0;

                        for (int i = 0; i < opcount; i++) {
                            action.before();
                            long st = System.currentTimeMillis();
                            int rescode = action.execute();
                            long en = System.currentTimeMillis();
                            accum += (en - st);
                            action.after();

                            if (rescode != HBaseClient.Ok) {
                                System.out.println("Error " + rescode + " for " + action.getKey());
                            }

                            if (i % 1 == 0) {
                                System.out.println(i + " operations, average latency: "
                                                   + (((double) accum) / ((double) i)));
                            }
                        }

                        System.out.println("Average latency: " + (((double) accum) / ((double) opcount)));
                        // System.out.println("Average get latency: "+(((double)cli.TotalGetTime)/((double)cli.TotalGetOps)));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            allthreads.add(t);
        }

        long st = System.currentTimeMillis();
        for (Thread t : allthreads) {
            t.start();
        }

        for (Thread t : allthreads) {
            try {
                t.join();
            } catch (InterruptedException e) {
            }
        }
        long en = System.currentTimeMillis();

        System.out.println("Throughput: " + ((1000.0) * (((double) (opcount * threadcount)) / ((double) (en - st))))
                           + " ops/sec");

    }
}
